package com.qianda.qdminiapp.controller.order;


import ch.qos.logback.classic.gaffer.PropertyUtil;
import com.qianda.qdminiapp.common.rabbitmq.MsgProducer;
import com.qianda.qdminiapp.controller.Result;
import com.qianda.qdminiapp.domain.dto.OrderAndPayDto;
import com.qianda.qdminiapp.domain.dto.PageRequest;
import com.qianda.qdminiapp.domain.model.order.Order;
import com.qianda.qdminiapp.domain.model.order.QqdOrderTowel;
import com.qianda.qdminiapp.security.SecurityUser;
import com.qianda.qdminiapp.service.PaymentContext;
import com.qianda.qdminiapp.service.QqdDeviceService;
import com.qianda.qdminiapp.service.QqdOrderTowelService;
import com.qianda.qdminiapp.service.TcpResponseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cw
 * @since 2019-08-06
 */
@RestController
@RequestMapping("/app_order")
public class OrderController {

    @Autowired
    private PaymentContext paymentContext;

    @Autowired
    private TcpResponseService tcpResponseService;

    @Autowired
    private QqdOrderTowelService qqdOrderTowelService;

    @Autowired
    private QqdDeviceService qqdDeviceService;

    @PostMapping("/order-pay")
    public Result createOrderAndPayment(@AuthenticationPrincipal SecurityUser user, @RequestBody OrderAndPayDto orderAndPayDto) throws InterruptedException {
        orderAndPayDto.setUserId(user.getId());

        //订单支付
        Map<String,Object> result = paymentContext.appPay(orderAndPayDto);

        //出巾指令丢入消息
        return Result.success(result);
    }


    @GetMapping("/getTcp")
    public Result getTcp(String tcpMsg){
        tcpResponseService.tcpResponseHandler(tcpMsg);
      return Result.success();

    }

    /*
     * 获取用户所有订单
     * @Description
     * @Author cw
     * @Date 11:12 2019/8/29
     * @Param [user]
     * @return com.qianda.qdminiapp.controller.Result
     **/
    @GetMapping("/findOrder")
    public Result findOrder(@AuthenticationPrincipal SecurityUser user, PageRequest pageRequest){
        return Result.success(qqdOrderTowelService.findOrder(user.getId(),pageRequest));

    }

    /*
     * 获取订单出巾结果
     * @Description
     * @Author cw
     * @Date 11:12 2019/8/29
     * @Param [user]
     * @return com.qianda.qdminiapp.controller.Result
     **/
    @GetMapping("/getOutResult/{id}")
    public Result getOutResult(@PathVariable Long id){
        return Result.success(qqdOrderTowelService.getById(id));
    }


    /*
     * 判断用户是否支持新用户活动
     * @Description
     * @Author cw
     * @Date 16:24 2019/9/3
     * @Param [user, imei]
     * @return com.qianda.qdminiapp.controller.Result
     **/
    @GetMapping("/getActivity")
    public Result getActivity(@AuthenticationPrincipal SecurityUser user,Long id){
        return Result.success(qqdDeviceService.checkIsNewActivity(user.getId(),id));
    }
}

